<?php
/**
 * 扫码富第三方支付类
 */

namespace app\api\service;

class SaoMaFuService
{

    public $domain = 'https://smf.jingweitech.cn:8443';

    /**
     * ASCII排序
     * @param array $params
     * @param $key
     * @return bool|string
     */
    public function ASCIISHA256($params, $key)
    {
        if (!empty($params)) {
            $params = array_filter($params);
            $p = ksort($params);
            if ($p) {
                $str = '';
                foreach ($params as $k => $val) {
                    $val = trim($val);
                    if ($val === '' || $val === null) {
                        continue;
                    }
                    $str .= $k . '=' . $val . '&';
                }
                $strs = rtrim($str, '&');
                $stringSignTemp = $strs . '&key=' . $key;

                $re = hash('sha256', $stringSignTemp, true);
                return strtoupper(bin2hex($re));
            }
        }
        return false;
    }


    /**
     * des 加密
     * str 需要加密的账号
     * key 加密秘钥
     */
    public function encryptStr($str, $key)
    {
        $data = mb_convert_encoding($str, 'UTF-8');
        $encry = openssl_encrypt($data, 'DES-ECB', $key, OPENSSL_RAW_DATA);
        return base64_encode($encry);
    }

    /**
     * 解密
     */
    public function decryptStr($data, $key)
    {
        $decry = openssl_decrypt(base64_decode($data), 'DES-ECB', $key, OPENSSL_RAW_DATA);
        return $decry;
    }
}